﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.Sql;
using System.Data.SqlClient;


namespace Database
{
   public class DonDatHang
    {
        private int maddh;

        public int Maddh
        {
            get { return maddh; }
            set { maddh = value; }
        }
        private string tenkh;

        public string Tenkh
        {
            get { return tenkh; }
            set { tenkh = value; }
        }
        private DateTime ngaydathang;

        public DateTime Ngaydathang
        {
            get { return ngaydathang; }
            set { ngaydathang = value; }
        }
        private bool tinhtrang;

        public bool Tinhtrang
        {
            get { return tinhtrang; }
            set { tinhtrang = value; }
        }        
        public DonDatHang()
        {
        }
        public DonDatHang(int maddh, string tenkh, DateTime ngaydathang, bool tinhtrang)
        {
            this.maddh = maddh;
            this.tenkh = tenkh;
            this.ngaydathang = ngaydathang;
            this.tinhtrang = tinhtrang;            
        }
        public static DataSet DSDDH()
        {
            DataSet ds = new DataSet();
            string query = "select * from dondathang";
            sqlConnection db = new sqlConnection();
            SqlDataAdapter sda = new SqlDataAdapter(query, db.GetConnect());
            sda.Fill(ds);
            return ds;
        }
        public static DataSet DSMaDDH()
        {
            DataSet ds = new DataSet();
            string query = "select maddh,tenkh from dondathang";
            sqlConnection db = new sqlConnection();
            SqlDataAdapter sda = new SqlDataAdapter(query, db.GetConnect());
            sda.Fill(ds);
            return ds;
        }
        public bool ThemDDH(string tenkh,DateTime ngaydathang,bool tinhtrang)
        {
            bool kq = true;

            try
            {
                string query = "insert into DonDatHang(tenkh,ngaydathang,tinhtrang) values('{0}','{1}','{2}')";
                query = String.Format(query,tenkh,ngaydathang,tinhtrang);
                sqlConnection db = new sqlConnection();
                SqlCommand cmd = new SqlCommand(query, db.GetConnect());
                return cmd.ExecuteNonQuery() > 0;
            }
            catch
            {
                kq = false;
            }

            return kq;
        }
        public bool CapNhatDDH(int maddh, string tenkh,DateTime ngaydathang, bool tinhtrang)
        {
            bool kq = true;

            try
            {
                string query = "update dondathang set tenkh='{0}',ngaydathang='{1}',tinhtrang='{2}'where maddh='{3}'";
                query = String.Format(query,tenkh,ngaydathang,tinhtrang,maddh);
                sqlConnection db = new sqlConnection();
                SqlCommand cmd = new SqlCommand(query, db.GetConnect());
                return cmd.ExecuteNonQuery() > 0;
            }
            catch
            {
                kq = false;
            }

            return kq;
        }
        public bool XoaDDH(int maddh)
        {
            bool kq = true;

            try
            {
                string query = "Delete dondathang where maddh='{0}'";
                query = String.Format(query,maddh);
                sqlConnection db = new sqlConnection();
                SqlCommand cmd = new SqlCommand(query, db.GetConnect());
                cmd.ExecuteNonQuery();
            }
            catch
            {
                kq = false;
            }

            return kq;
        }
        public DonDatHang ThongTinDDH(int maddh)
        {
            string query = "select * from dondathang where maddh=" +maddh;
            sqlConnection db = new sqlConnection();
            SqlCommand sc = new SqlCommand(query, db.GetConnect());
            SqlDataReader reader = sc.ExecuteReader();
            DonDatHang ddh = new DonDatHang();
            while (reader.Read())
            {
                ddh.maddh = reader.GetInt32(0);
                ddh.tenkh = reader.GetString(1);
                ddh.ngaydathang = reader.GetDateTime(2);
                ddh.tinhtrang = reader.GetBoolean(3);                              
            }
            return ddh;
        }
        public void ThemDonDatHang(DonDatHang add)
        {
            SqlCommand cmd;
            try
            {
                sqlConnection db = new sqlConnection();
                cmd = new SqlCommand("insert into DonDatHang(tenkh,ngaydathang,tinhtrang) values(@tenkh,@ngaydathang,@tinhtrang)", db.GetConnect());
                cmd.Parameters.Add("@tenkh", SqlDbType.NVarChar).Value = add.tenkh;
                cmd.Parameters.Add("@ngaydathang", SqlDbType.DateTime).Value = add.ngaydathang;
                cmd.Parameters.Add("@tinhtrang", SqlDbType.Bit).Value = add.tinhtrang;

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public DataTable DSDDHMoiNhat()
        {
            DataSet ds = new DataSet();
            try
            {
                string query = "select top 1 maddh from DonDatHang order by maddh desc";
                sqlConnection db = new sqlConnection();
                SqlDataAdapter sda = new SqlDataAdapter(query, db.GetConnect());
                sda.Fill(ds);

            }
            catch
            {
                ds = null;
            }
            return ds.Tables[0];
        }
    }
}
